package cn.taobaoke.restful.controller;

import cn.taobaoke.restful.constant.Constants;
import cn.taobaoke.restful.domain.AdminEntity;
import cn.taobaoke.restful.domain.AgentEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;

/**
 * @author yangzuo
 * @since 2017-03-11
 */
@RestController
public class BaseController {

    @Resource
    protected HttpServletRequest request;

    @Resource
    protected HttpServletResponse response;

    @Resource
    protected HttpSession session;

    public AdminEntity getLoginAdmin() {
        return (AdminEntity) session.getAttribute(Constants.LoginUser.ADMIN);
    }

    public Long getLoginAdminId() {
        AdminEntity admin = getLoginAdmin();
        return admin == null ? null : admin.getId();
    }

    public AgentEntity getLoginAgent() {
        return (AgentEntity) session.getAttribute(Constants.LoginUser.AGENT);
    }

    public Long getLoginAgentId() {
        AgentEntity agent = getLoginAgent();
        return agent == null ? null : agent.getId();
    }

    public Pageable createPageable(Integer page, Integer size, String sort, Sort.Direction direction) {
        page = page == null || page <= 0 ? 0 : page - 1;
        size = size == null || size < 0 || size > 1000 ? 20 : size;

        return PageRequest.of(page, size, new Sort(direction, sort));
//        return new PageRequest(page, size, new Sort(direction, sort));
    }

    public Pageable createPageable(Integer page, Integer size, String sortFeild, String direction) {
        sortFeild = sortFeild == null ? "id" : sortFeild;
        direction = direction == null ? "desc" : direction;
        page = page == null || page <= 0 ? 0 : page - 1;
        size = size == null || size < 0 || size > 1000 ? 20 : size;

        return PageRequest.of(page, size, new Sort(Sort.Direction.fromString(direction), sortFeild));
//        return new PageRequest(page, size, new Sort(Sort.Direction.fromString(direction), sortFeild));
    }

    public Pageable createPageable(Integer page, Integer size) {
        page = page == null || page <= 0 ? 0 : page - 1;
        size = size == null || size < 0 || size > 1000 ? 20 : size;

        return PageRequest.of(page, size, new Sort(Sort.Direction.DESC, "id"));
//        return new PageRequest(page, size, new Sort(Sort.Direction.DESC, "id"));
    }

    public class Result extends HashMap<String, Object> {
        public Result() {
            this.put("success", true);
        }

        public Result(Object data) {
            this.put("success", true);
            this.put("data", data);
        }

        public Result(Integer currentPage, Integer pageSize, Long total, Integer totalPage, Object data) {
            this.put("success", true);
            this.put("data", data);

            HashMap<String, Object> pageMap = new HashMap<>();
            pageMap.put("current", currentPage + 1);
            pageMap.put("pageSize", pageSize);
            pageMap.put("total", total);
            pageMap.put("totalPage", totalPage);

            this.put("page", pageMap);
        }
    }

    protected Result wrapError(String error) {
        Result result = new Result();
        result.put("success", false);
        result.put("message", error);
        return result;
    }

    protected Result wrapVoid() {
        return new Result();
    }

    protected Result wrapObj(Object obj) {
        return new Result(obj);
    }

    protected Result wrapPage(Integer currentPage, Integer pageSize, Long total, Object data) {
        return new Result(currentPage, pageSize, total, (total.intValue() + pageSize - 1) / pageSize, data);
    }

    protected Result wrapPage(Integer currentPage, Integer pageSize, Page page) {
        return new Result(currentPage, pageSize, page.getTotalElements(), page.getTotalPages(), page.getContent());
    }

    protected Result wrapPage(Pageable pageable, Page page) {
        return new Result(pageable.getPageNumber(), pageable.getPageSize(), page.getTotalElements(), page.getTotalPages(), page.getContent());
    }
}
